package com.chenjl.transport.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chenjl.transport.HookChannelHandler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelHandler.Sharable;
/**
 * 
 * 2016-8-30 13:20:34
 * @author chenjinlong
 */
@Sharable
public class ClientHandler extends ChannelInboundHandlerAdapter {
	private static final Logger logger = LoggerFactory.getLogger(ClientHandler.class);
	private HookChannelHandler hookChannelHandler;
	
	public ClientHandler(HookChannelHandler hookChannelHandler) {
		this.hookChannelHandler = hookChannelHandler;
	}
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		//连接到服务端
		logger.info("client channelActive");
		hookChannelHandler.channelActive(ctx.channel());
	}
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		//服务端断开
		logger.info("client channelInactive");
		hookChannelHandler.channelInactive(ctx.channel());
	}
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		logger.info("client channelRead begin, msg class : {}",msg.getClass());
		hookChannelHandler.channelRead(ctx.channel(),msg);
	}
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		logger.info("client exceptionCaught");
		hookChannelHandler.exceptionCaught(ctx.channel(),cause);
		cause.printStackTrace();
		ctx.close();
	}
}